package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.BaseCategory1Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory2Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory3Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategoryViewMapper;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.anno.GuiguCache;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.vo.album.AlbumCategoryVo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;


	@Override
	public List<AlbumCategoryVo> findAllCategory() {

	/*	//创建list集合对象,用于存储三个Id的最终结果
		List<AlbumCategoryVo> list = new ArrayList<>();
		//查询视图获取所有的分类数据
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.findAllCategory(null);
		//按照一级分类Id进行分组 key:一级分类Id value:一级分类Id对应的集合数据
		Map<Long,List<BaseCategoryView>> map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

		//遍历Map的常见方式,从一个map中对象获取已有的键值对 并将每个键值对表示为一个Map.Entry<long,List<BaseCategoryView>对象.
		//long表示键的数据类型 list<BaseCategoryView>表示值的数据类型 调用entrySet()方式可以获得所有键值对的set集合,然后使用一个foreach循环来遍历
		//entry变量代表一个键值对 entry.getKey 和 entry.getValue可以分别获取值
		for(Map.Entry<Long, List<BaseCategoryView>> entry : map.entrySet()){
			//获取到一级分类Id
			Long category1Id = entry.getKey();

			//获取到一级分类Id 对应的集合数据
			List<BaseCategoryView> categoryViewList = entry.getValue();

			//声明一级分类对象
			AlbumCategoryVo category1 = new AlbumCategoryVo();
			category1.setCategoryId(category1Id);
			category1.setCategoryName(categoryViewList.get(0).getCategory1Name());

			//按照二级分类Id 进行分组 .collect将流转换位map对象
			//Map<Long, List<BaseCategoryView>> map2 = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Map<Long, List<BaseCategoryView>> map2 = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			//声明二级分类对象 [集合]
			List<AlbumCategoryVo> category2Child  = new ArrayList<>();

			//循环遍历
			for(Map.Entry<Long, List<BaseCategoryView>> entry2 : map2.entrySet()){
				//获取到二级分类的Id
				Long category2Id = entry2.getKey();

				//获取到二级分类Id 对应的集合数据
				List<BaseCategoryView> category2ViewList = entry2.getValue();

				//创建二级分类对象
				AlbumCategoryVo category2 = new AlbumCategoryVo();
				category2.setCategoryId(category2Id);

				category2.setCategoryName(category2ViewList.get(0).getCategory2Name());

				//循环三级分类数据
				List<AlbumCategoryVo> categoryList3Child = category2ViewList.stream().map(baseCategoryView -> {
					AlbumCategoryVo category3 = new AlbumCategoryVo();
					category3.setCategoryId(baseCategoryView.getCategory3Id());
					category3.setCategoryName(baseCategoryView.getCategory3Name());
					return category3;
				}).collect(Collectors.toList());
				//将二级分类数据对象添加到集合中
				//为了能把三个数据放到一个大集合里面 先把二级对象的数据 放在二级集合
				category2Child.add(category2);
				//将三级数据放入到二级里面
				category2.setCategoryChild(categoryList3Child);
			}
			//将二级数据放入到一级里面
			category1.setCategoryChild(category2Child);

			//一级分类放入集合中
			//集合要圆满category1要有子集合  就是category2Child
			//category2Child要有2的数据对象 加上三数据的子集合
			list.add(category1);
		}
			return list;
	}*/
		return baseCategoryViewMapper.findAllCategory();
	}

	// 加入了查询缓存的功能，并且使用了分布式锁解决了缓存击穿问题
	// 加入了查询缓存的功能，并且使用了分布式锁解决了缓存击穿问题
	@GuiguCache(
			cacheKey = "album:category:view:" + "#{#params[0]}" ,
			enableLock = true ,
			lockName = "album:category:view:lock:" + "#{#params[0]}"
	)
	@Override
	public BaseCategoryView findCategoryByC3Id(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}

	@Override
	public AlbumCategoryVo getBaseCategoryList(Long category1Id) {
		return baseCategoryViewMapper.getBaseCategoryList(category1Id);
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		// 根据一级分类的id查询所有的二级分类数据中的ids
		LambdaQueryWrapper<BaseCategory2> baseCategory2LambdaQueryWrapper = new LambdaQueryWrapper<BaseCategory2>() ;
		baseCategory2LambdaQueryWrapper.eq(BaseCategory2::getCategory1Id,category1Id) ;
		List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(baseCategory2LambdaQueryWrapper);
		List<Long> category2Ids = baseCategory2List.stream().map(baseCategory2 -> baseCategory2.getId()).collect(Collectors.toList());

		// 根据二级分类的id查询置顶的三级分类数据
		LambdaQueryWrapper<BaseCategory3> baseCategory3LambdaQueryWrapper = new LambdaQueryWrapper<>()  ;
		baseCategory3LambdaQueryWrapper.in(BaseCategory3::getCategory2Id,category2Ids) ;
		baseCategory3LambdaQueryWrapper.eq(BaseCategory3::getIsTop,1) ;
		baseCategory3LambdaQueryWrapper.orderByAsc(BaseCategory3::getId) ;
		baseCategory3LambdaQueryWrapper.last("limit 7") ;
		List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(baseCategory3LambdaQueryWrapper);

		return baseCategory3List ;
	}


}
